#include<bits/stdc++.h>
using namespace std;
int a[200002];
int h[1000002];
int f[200002];
int ff[200002];
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int T;
    cin>>T;
    for(int qwq=1;qwq<=T;qwq++){
        int n;
        cin>>n;
        memset(h,0,sizeof(h));
        f[0]=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            if(h[a[i]]==0)h[a[i]]=i;
            if(abs(h[a[i]]-i)!=0){
                if(f[h[a[i]]]+a[i]>f[i-1]){
                    f[i]=f[h[a[i]]-1]+a[i];
                    ff[i]=h[a[i]];
                }
                else {
                    f[i]=f[i-1];
                    ff[i]=i-1;
                }
            }
            else {
                f[i]=f[i-1];
                ff[i]=i-1;
            }
            h[a[i]]=i;
        }
        // for(int i=1;i<=n;i++)cout<<f[i]<<' ';
        // cout<<endl;
        long long ans=f[n];
        int lasti=ff[n];
        for(int i=n;i!=0;i=ff[i]){
            a[i]=-1;
            if(lasti-i>=3){
                for(int j=i+2;j<=lasti-2;j++){
                    a[j]=-1;
                }
            }
            lasti=ff[i];
        }
        int last=-1;
        for(int i=1;i<=n;i++){
            if(a[i]!=-1){
                if(last==a[i])ans+=a[i];
                last=a[i];
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}